//
//  ShangjiaDingdanGuanliViewController.swift
//  联付商圈
//
//  Created by zhangqi on 2017/8/26.
//  Copyright © 2017年 lianfu. All rights reserved.
//

import UIKit
import Alamofire

class ShangjiaDingdanGuanliViewController: BaseViewController {

    var yiwanchengArr = [[String:Any]]()
    var weiwanchengArr = [[String:Any]]()
    
    @IBOutlet weak var tabBar: UITabBar!
    
    
    @IBOutlet weak var oneItem: UITabBarItem!
    
    
    @IBOutlet weak var twoItem: UITabBarItem!
    
    
    @IBOutlet weak var xiahuaxianView: UIView!
    
    
    @IBOutlet weak var ddTableView: UITableView!
    
    @IBOutlet weak var wddTableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tabBar.selectedItem = oneItem
        ddTableView.tag = 101
        wddTableView.tag = 102
        ddTableView.isHidden = false
        wddTableView.isHidden = true
        ddTableView.tableFooterView = UIView(frame: CGRect.zero)
        wddTableView.tableFooterView = UIView(frame: CGRect.zero)
        tabBar.tintColor = UIColor(red: 84/255, green: 155/255, blue: 219/255, alpha: 1)
        xiahuaxianView.frame = CGRect(x: 0, y: 110, width: mainScreenW/2, height:3)
        yiwancheng()
        weiwancheng()
    }

    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.navigationBar.isHidden = false
        //xiahuaxianView.frame = CGRect(x: 0, y: 110, width: mainScreenW/2, height:3)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    func yiwancheng(){
        self.yiwanchengArr = []
        //self.ddTableView.reloadData()
        let x = UserDefaults.standard.object(forKey: "userInfo") as! NSDictionary
        let y = x["name"]!
        let yhid = ["name":"\(y)"]
        let urlString = "https://www.beidikeji.com/life/public/jiekou/dingdanuse?"
        let manager = Alamofire.SessionManager.default
        manager.session.configuration.timeoutIntervalForRequest = 45
        manager.request(urlString,method:.post,parameters: yhid).responseJSON {Request in
            switch Request.result {
            case .success:
                let json = JSON(data: Request.data!)
                //print(json)
                self.yiwanchengArr = json.arrayObject as! [[String : Any]]
                self.ddTableView.reloadData()
            case .failure:
                self.noticeInfo("未知错误", autoClear: true, autoClearTime: 1)
            }
        }
    }

    func weiwancheng(){
        self.weiwanchengArr = []
        //self.ddTableView.reloadData()
        let x = UserDefaults.standard.object(forKey: "userInfo") as! NSDictionary
        let y = x["name"]!
        let yhid = ["name":"\(y)"]
        let urlString = "https://www.beidikeji.com/life/public/jiekou/dingdannouse?"
        let manager = Alamofire.SessionManager.default
        manager.session.configuration.timeoutIntervalForRequest = 45
        manager.request(urlString,method:.post,parameters: yhid).responseJSON {Request in
            switch Request.result {
            case .success:
                let json = JSON(data: Request.data!)
//                print(y)
//                print(json)
                self.weiwanchengArr = json.arrayObject as! [[String : Any]]
                self.wddTableView.reloadData()
            case .failure:
                self.noticeInfo("未知错误", autoClear: true, autoClearTime: 1)
            }
        }
    }
    
    func gaibianzhuangtai(dh:String){
        let par = ["ddh":"\(dh)"]
        let urlString = "https://www.beidikeji.com/life/public/jiekou/dingdanchange?"
        let manager = Alamofire.SessionManager.default
        manager.session.configuration.timeoutIntervalForRequest = 45
        manager.request(urlString,method:.post,parameters: par).responseJSON {Request in
            switch Request.result {
            case .success:
                _ = JSON(data: Request.data!)
//                print(json)
//                print(json)
                self.weiwanchengArr = []
                self.weiwancheng()
            case .failure:
                self.noticeInfo("未知错误", autoClear: true, autoClearTime: 1)
            }
        }
    }
}





extension ShangjiaDingdanGuanliViewController: UITabBarDelegate, UITableViewDelegate, UITableViewDataSource {
    
    func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        switch item.tag {
        case 4:
            UITableView.animate(withDuration: 0.2, animations: {
                self.xiahuaxianView.frame = CGRect(x: 0, y: 110, width: mainScreenW/2, height:3)})
            //showdingdan()
            yiwancheng()
            ddTableView.isHidden = false
            wddTableView.isHidden = true
//            ddTableView.reloadData()
//            wddTableView.reloadData()
        case 5:
            UITableView.animate(withDuration: 0.2, animations: {
                self.xiahuaxianView.frame = CGRect(x: mainScreenW/2, y: 110, width: mainScreenW/2, height:3)})
            //weizhifudingdan()
            weiwancheng()
            ddTableView.isHidden = true
            wddTableView.isHidden = false
//            ddTableView.reloadData()
//            wddTableView.reloadData()
        default:
            break
        }
    }
    

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView.tag == 101 {
            return yiwanchengArr.count
        } else {
            return weiwanchengArr.count
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if tableView.tag == 101 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ShangjiaDingdanGuanliTableViewCell", for: indexPath) as! ShangjiaDingdanGuanliTableViewCell
            cell.ddhLab.text = "提交时间:" + "\(yiwanchengArr[indexPath.row]["created_at"]!)"//"订单号:" + "\(yiwanchengArr[indexPath.row]["ddh"]!)"
            cell.moneyLab.text = "¥" + "\(yiwanchengArr[indexPath.row]["allprice"]!)"
            cell.timeLab.text = "订单号:" + "\(yiwanchengArr[indexPath.row]["ddh"]!)"//"提交时间:" + "\(yiwanchengArr[indexPath.row]["created_at"]!)"
            cell.totalLab.text = "\(yiwanchengArr[indexPath.row]["name"]!)" + "等，共计" + "\(yiwanchengArr[indexPath.row]["number"]!)" + "款商品"
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ShangjiaDingdanweiwanchengTableViewCell", for: indexPath) as! ShangjiaDingdanweiwanchengTableViewCell
            cell.ddhLab.text = "订单号:" + "\(weiwanchengArr[indexPath.row]["ddh"]!)"
            cell.jiageLab.text = "¥" + "\(weiwanchengArr[indexPath.row]["allprice"]!)"
            cell.titleLab.text = "提交时间:" + "\(weiwanchengArr[indexPath.row]["created_at"]!)"
            cell.totalLab.text = "\(weiwanchengArr[indexPath.row]["name"]!)" + "等，共计" + "\(weiwanchengArr[indexPath.row]["number"]!)" + "款商品"
            return cell
        }

        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        ddTableView.deselectRow(at: indexPath, animated: false)
        wddTableView.deselectRow(at: indexPath, animated: false)
        if tableView.tag == 101 {
            ddh = "\(yiwanchengArr[indexPath.row]["ddh"]!)"
        } else if tableView.tag == 102 {
            ddh = "\(weiwanchengArr[indexPath.row]["ddh"]!)"
        }
        
        
        let sb = UIStoryboard(name: "Main", bundle:nil)
        let secondViewController = sb.instantiateViewController(withIdentifier: "dingdanxiangqing") as! UINavigationController
        self.present(secondViewController, animated: true, completion: nil)
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 20
        
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 65
    }
  
    
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        wddTableView.deleteRows(at: [indexPath as IndexPath], with: .fade)
    }
    
    
    
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        if tableView.tag == 102 {
            let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.default, title: "完成",handler: { (action, indexPath) -> Void in
                let x = self.weiwanchengArr.count
                if x == 0 {
                    return
                } else if x != 0 {
                    let x = "\(self.weiwanchengArr[indexPath.row]["ddh"]!)"
                    self.gaibianzhuangtai(dh:x)
                }
                
            })
            return [deleteAction]
        } else {
            return nil
        }
    }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        if tableView.tag == 102 {
            return true
        } else {
            return false
        }
        
    }

    
}

